<%@ Control Language="C#" Inherits="SoftwareMonkeys.Scriptech.Web.BaseScript" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Configuration" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Projects" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Web" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Ftp" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Diagnostics" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Business" %>
<%@ Import namespace="System.IO" %>
<script runat="server">
private void Page_Load(object sender, EventArgs e)
{
	using (LogGroup logGroup = LogGroup.StartDebug("Loading the create project script."))
	{
		if (!IsPostBack)
		{
			FtpServer[] servers = IndexStrategy.New<FtpServer>(false).Index<FtpServer>();
			ReportsFtpServer.DataSource = servers;
			StagingFtpServer.DataSource = servers;
	
			DataBind();
		}
	}
}

private void CreateButton_Click(object sender, EventArgs e)
{
	Project project = null;
	using (LogGroup logGroup = LogGroup.StartDebug("Creating a new project."))
	{
		project = ProjectCreator.CreateProject(ProjectName.Text.Trim());
	
		LogWriter.Debug("Project name: " + project.Name);
		LogWriter.Debug("Assemblies directory: " + project.AssembliesDirectory);
		LogWriter.Debug("Reports server ID: " + project.ReportsServerID.ToString());
	
		project.AssembliesDirectory = AssembliesFolder.Text.Trim();
		project.BuildOrder = BuildOrder.Text.Trim().Split('\n');
		project.ReportsServerID = (ReportsFtpServer.SelectedValue != null && ReportsFtpServer.SelectedValue != String.Empty)
			? new Guid(ReportsFtpServer.SelectedValue)
			: Guid.Empty;
		project.StagingServerID = (StagingFtpServer.SelectedValue != null && StagingFtpServer.SelectedValue != String.Empty)
			? new Guid(StagingFtpServer.SelectedValue)
			: Guid.Empty;
	
		ProjectSaver.SaveProject(Request["Store"],
			Request["Group"],
			project);
	}
	if (project != null)
		Response.Redirect(ScriptPath.GetUrl("Project.script", "Project=" + project.Name, "Group", "Store"));

}
</script>
	<h1>Create Project</h1>
<hr/>
<div><a href='<%= Request.ApplicationPath %>'>Home</a> - <a href='<%= ScriptPath.GetUrl("StoreIndex.script") %>'>Stores</a>  - <a href='<%= ScriptPath.GetUrl("GroupIndex.script", "Store") %>'>Groups</a> - <a href='<%= ScriptPath.GetUrl("ProjectIndex.script", "Store", "Project", "Group") %>'>Projects</a></div>
<hr/>
	<table width="100%">
		<tr>
			<td>Store name:</td>
			<td>
				<%= Request["Store"] %></td>
		</tr>
		<tr>
			<td>Group name:</td>
			<td>
				<%= Request["Group"] %></td>
		</tr>
		<tr>
			<td>Project name:</td>
			<td>
				<asp:textbox runat="server" id="ProjectName" width="400px" text="newproject"/></td>
		</tr>
		<tr>
			<td>Assemblies folder:</td>
			<td>
				<asp:textbox runat="server" id="AssembliesFolder" width="400px" text="src"/></td>
		</tr>
		<tr>
			<td>Build order (one per line):</td>
			<td>
				<asp:textbox runat="server" id="BuildOrder" TextMode="MultiLine" Rows="8" width="400px"/></td>
		</tr>
		<tr>
			<td>Reports FTP server:</td>
			<td>
				<asp:DropDownList runat="Server" id="ReportsFtpServer" DataTextField="Name" DataValueField="ID" /></td>
		</tr>
		<tr>
			<td>Staging FTP server:</td>
			<td>
				<asp:DropDownList runat="Server" id="StagingFtpServer" DataTextField="Name" DataValueField="ID" /></td>
		</tr>
		<tr>
			<td> </td>
			<td>
				<asp:button runat="server" id="CreateButton" text="Create" onclick="CreateButton_Click"/>
			</td>
		</tr>
	</table>